package com.zhiwang.planet.ui.my

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.inputmethod.InputMethodManager
import com.zhiwang.planet.R
import com.zhiwang.planet.di.component.DaggerUserComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.mvp.p.ScalarPresenter
import com.zhiwang.planet.mvvm.model.AppDataBase
import com.zhiwang.planet.mvvm.model.entity.User
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.ui.my.reward.VerifyCardActivity
import com.zhiwang.planet.util.Toaster
import com.zhiwang.planet.util.quickStartActivity
import com.zhiwang.planet.widget.SimpleDialogBuilder
import kotlinx.android.synthetic.main.activity_bonus_transform.*
import javax.inject.Inject

class BonusTransformActivity : BaseActivity() {


    var forSelf = true

    var userInfo: User? = null


    @Inject
    lateinit var presenter: ScalarPresenter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        DaggerUserComponent.builder()
                .appComponent(getAppComponent())
                .accountModule(AccountModule(this))
                .autoDisposeModule(AutoDisposeModule(this))
                .build()
                .inject(this)
        setContentView(R.layout.activity_bonus_transform)
        setSupportActionBar(toolbar)
        init()
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }


    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.bonus_card_verify, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item?.itemId == R.id.verify_card) {
            quickStartActivity(VerifyCardActivity::class.java)
        }
        return super.onOptionsItemSelected(item)
    }

    @SuppressLint("SetTextI18n")
    private fun init() {
//        userInfo = AppDataBase.getInstance(this)
//                .userDao()
//                .getFirstUser()
//        et_userid.setText(userInfo?.userId)
//        et_userid.background.alpha = 0
//        et_userid.isEnabled = false
//
//        et_userid.setOnFocusChangeListener { v, hasFocus ->
//            if (forSelf.not() && hasFocus.not()) {
//                presenter.checkUserExists(et_userid.text.toString().trim(),::onUserNotExist)
//            }
//        }
    }

    private fun onUserNotExist(throwable: Throwable) {
        Toaster.message("好友id不存在")
    }

    fun switchAccount(view: View) {

        if (forSelf) {
            forSelf = false
            switch_btn.text = "为自己开通>>"
            et_userid.setText("")
            et_userid.hint = "请输入好友id"
            et_userid.background.alpha = 255
            et_userid.isEnabled = true
        } else {
            forSelf = true
            switch_btn.text = "为好友开通>>"
            et_userid.setText(userInfo?.userId)
            et_userid.background.alpha = 0
            et_userid.isEnabled = false

        }
    }

    fun closeIme() {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(window.decorView.windowToken, 0)
    }


    /**
     * 确认兑换
     */
    fun confirm(view: View) {

        val welfare = et_welfare.text.toString()
        val friendId = if (forSelf) "" else et_userid.text.toString()

        val userid = if (forSelf) userInfo?.userId else friendId
//
//        if (forSelf.not() && friendId.isEmpty()) {
//            Toasty.message("好友id 不能为空")
//            return
//        }

        presenter.exchange(userid = userid
                ?: "", friendId = friendId, cardsList = welfare, onMsg = ::showFinalMsg, onError = ::onError)

    }

    fun showFinalMsg(msg: String) {
        SimpleDialogBuilder()
                .title("")
                .content(msg)
                .buttonText("我知道了")
                .build(this)
                .show()

    }

    fun transform(view: View) {
        val welfare = et_welfare.text.toString()
        val friendId = if (forSelf) "" else et_userid.text.toString()
        presenter.transformCard(cardNumber = welfare, onMsg = ::showFinalMsg, onError = ::onError)
    }
}
